<?php

class PlaceController extends BaseController{
	function placesList(){
		$places = Place::all();
		return View::make('place.placesList', $data = array('places' => $places));
	}

	function updatePlace(){
		//function chay vao sau khi click nut update
		if(Auth::check()){
			Input::merge(array_map('trim', Input::all()));
			$id = ID::find(Input::get('id'));
			$rules = array(
				'place_name' 	=> 'required',
				'longitude'		=> 'required',
				'latitude'	 	=> 'required'
			);
			$validator = Validator::make(Input::all(), $rules);
			if ($validator->fails()) {
				return Redirect::to('place/edit/'.$place->id)
					->withErrors($validator) 
					->withInput(); 
			} else {
				
				//$id = new ID();
				//$id->id_type_id = 2;
				//$id->save();

				//$id->place = new Place();
				//$id->place->id = $id->id;
				$id->place->place_name = Input::get('place_name');
				$id->place->place_description = Input::get('place_description');
				//$id->place->longitude = Input::get('longitude');
				//$id->place->latitude = Input::get('latitude');
				//$id->place->place_verify = 0;
				//$id->place->place_shaphira_rate = 0;
				$id->place->updated_by = Auth::user()->id;
				//$id->place->save();

				//$id->place->placeInformation = new PlaceInformation();
				//$id->place->placeInformation->id = $id->id;
				$id->place->placeInformation->hotline = Input::get('hotline');
				$id->place->placeInformation->tel_no = Input::get('tel_no');
				$id->place->placeInformation->website = Input::get('website');
				$id->place->placeInformation->email = Input::get('email');
				$id->place->placeInformation->description = Input::get('description');
				$id->place->placeInformation->open_time = Input::get('open_time');
				$id->place->placeInformation->close_time = Input::get('close_time');

				//$id->place->placeInformation->save();
				$id->push();


		    	return Redirect::to('place/'.$id->id);
			}
		} else{
			return Redirect::to('login/');
		}
	}

	function insertPlace(){
		//function chay sau khi click nut insert
		//insert nhanh, chap nhan chi can name va longitude, latitude
		if(Auth::check()){
			Input::merge(array_map('trim', Input::all()));
			$rules = array(
				'place_name' 	=> 'required',
				'longitude'		=> 'required',
				'latitude'	 	=> 'required'
			);
			$validator = Validator::make(Input::all(), $rules);
			if ($validator->fails()) {
				return Redirect::to('place/new')
					->withErrors($validator) 
					->withInput(); 
			} else {
				
				$id = new ID();
				$id->id_type_id = 2;
				$id->save();

				$id->place = new Place();
				$id->place->id = $id->id;
				$id->place->place_name = Input::get('place_name');
				$id->place->place_description = Input::get('place_description');
				$id->place->longitude = Input::get('longitude');
				$id->place->latitude = Input::get('latitude');
				$id->place->place_verify = 0;
				$id->place->place_shaphira_rate = 0;
				$id->place->created_by = Auth::user()->id;
				$id->place->save();

				$id->place->placeInformation = new PlaceInformation();
				$id->place->placeInformation->id = $id->id;
				$id->place->placeInformation->hotline = Input::get('hotline');
				$id->place->placeInformation->tel_no = Input::get('tel_no');
				$id->place->placeInformation->website = Input::get('website');
				$id->place->placeInformation->email = Input::get('email');
				$id->place->placeInformation->description = Input::get('description');
				$id->place->placeInformation->open_time = Input::get('open_time');
				$id->place->placeInformation->close_time = Input::get('close_time');

				$id->place->placeInformation->save();


		    	return Redirect::to('place/'.$id->id);
			}
		} else{
			return Redirect::to('login/');
		}
	}

	function searchByName($stringKey = ''){
		$places = Place::where('place_name', '=', 'Văn phòng Shaphira')->get(); //$stringKey); //Chua sua lai chinh xac cua lenh thanh dang *stringKey*
		return Response::json(array('places' => $places));
	}

	function getNealyPlace($longitude, $latitude){
		$num = 5;
		$longtitude = round($longtitude,$num);
		$latitude = round($latitude,$num);
		$range = 2*pow(10,-$num);
		$nealyPlaces = Place::whereRaw('longtitude > ? and longtitude < ? and latitude > ? and latitude < ?',array($longtitude-$range, $longtitude+$range, $latitude-$range, $latitude+$range))->get();
		$result = array();
		foreach ($nealyPlaces as $place) {
			if($place->getDistance($longtitude,$latitude)<=0.2){
				array_push($result, $place);
			}
		}
		return Response::json(array('places'=>$result));
	}

	/*function calculateDistance($latitude1= 0, $longtitude1= 0, $latitude2 = 0, $longtitude2 = 0){
		$circumference = 400075.6;
		$distance = 0.0;
		$latitude1Rad = deg2rad($latitude1);
		$latitude2Rad = deg2rad($latitude2);
		$longitude1Rad = deg2rad($longitude1);
		$longitude2Rad = deg2rad($longitude2);

		$longitudeDiff = abs($longitude1Rad-$longitude2Rad);

		if($longitudeDiff > pi()){
			$longitudeDiff = pi()*2.0 - $longitudeDiff;
		}

		$angleCalculation = acos(sin($latitude1Rad)*sin($latitude2Rad)+cos($latitude1Rad)*cos($latitude2Rad)*cos($longitudeDiff));

		$distance = $circumference * $angleCalculation / (pi()*2.0);
		return Response::json(array('distance' => $distance));
	}*/
}